#include<pthread.h>
#include<stdio.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

int queue[10];
int count=0;
void* comsumer(void*arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        while(count==0)
        {
            pthread_cond_wait(&cond,&mutex);
        }
        int data=queue[--count];
        printf("消费数据:%d\n",data);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
void *producer(void* arg)
{
    for(int i=0;i<5;i++)
    {
        pthread_mutex_lock(&mutex);
        queue[count++]=i;
        printf("生产数据:%d\n",i);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
int main()
{
    pthread_t c,p;
    pthread_create(&c,NULL,comsumer,NULL);
    pthread_create(&p,NULL,producer,NULL);
    pthread_join(c,NULL);
    pthread_join(p,NULL);
    return 0;
}